﻿using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using XFControls.Utils;
using XFControls.XFPanels;
using XFControls.XFPanels.XFPanelItems;
using System;

namespace XFControls
{
#pragma warning disable 1591
    public partial class XFPanelContainer : UserControl
    {
        private readonly List<XFPanelBase> _overlayPanels;
        private bool _allowTextInput;

        public XFPanelContainer()
        {
            InitializeComponent();

            SIPHeight = _inputPanel.Bounds.Height;
            mainPanel = new XFPanelBase { Width = ClientSize.Width };

            _inputPanel.EnabledChanged += InputPanelEnableChange;

            _overlayPanels = new List<XFPanelBase>();
        }

        private void InputPanelEnableChange(object sender, EventArgs args)
        {
            if (_allowTextInput)
                return;

            if (_textInput == null)
            {
                //_inputPanel.Enabled = false;
                return;
            }
            int width = Screen.PrimaryScreen.WorkingArea.Width;
            int padding = width / 20;
            int buttonPadding = padding + (padding / 2);

            int height = _inputPanel.VisibleDesktop.Height -
                         ((padding * 2) + Header.Height + _submit.Height);

            _textInput.Height = height;
            _submit.Location = new Point(padding, height + buttonPadding);
            _cancel.Location = new Point(width - (_cancel.Width + padding),
                                         height + buttonPadding);

            _textInput.Invalidate();
        }

        public int SIPHeight { get; private set; }

        public XFPanelBase MainPanel
        {
            get { return mainPanel; }
        }

        public XFPanelHeader Header
        {
            get { return header; }
        }

        public IUpdateable UpdateNotification { get; set; }

        public void Reset()
        {
            mainPanel.BringToFront();
            //mainPanel.Reset();
        }

        public void SetMainPanel(XFPanelBase panel)
        {
            if (mainPanel.RightPanel == null && !mainPanel.SavePanelAfterSlide)
                mainPanel.Dispose();

            mainPanel = panel;
            mainPanel.Width = ClientSize.Width;
            if (!Controls.Contains(panel))
                Controls.Add(panel);

            if (!string.IsNullOrEmpty(panel.PanelName))
                SetHeaderText(panel.PanelName);

            BackColor = panel.BackColor;

            Reset();
        }

        public void AddOverlay(XFPanelBase panel)
        {
            _overlayPanels.Add(panel);
        }

        public void ShowScrollbar(bool show)
        {
            mainPanel.ShowScrollbar(show);
        }

        public void ShowAlphaBar(bool show)
        {
            mainPanel.ShowAlphaBar(show);
        }

        public void SetHeader(XFPanelHeader xfPanelHeader)
        {
            header = xfPanelHeader;
            xfPanelHeader.MainPanelContainer = this;

            if (Parent.Controls.Contains(xfPanelHeader)) return;

            Parent.Controls.Add(xfPanelHeader);
            xfPanelHeader.Dock = DockStyle.Top;
        }

        public void SetHeaderText(string text)
        {
            if (header == null) return;

            header.CenterText = text;
            header.Invalidate();
        }

        public string GetHeaderText()
        {
            if (header != null)
            {
                return header.CenterText;
            }
            return null;
        }

        public void SetHeaderRightButton(IXFButton button)
        {
            if (header != null)
            {
                button.Parent = header;
                header.RightButton = button;
                header.Invalidate();
            }
        }

        public void SetHeaderLeftButton(IXFButton button)
        {
            if (header != null)
            {
                if (button != null)
                    button.Parent = header;
                header.LeftButton = button;
                header.Invalidate();
            }
        }

        public void ShowTextBox()
        {
            _allowTextInput = true;
            Application.DoEvents();

            while (!_inputPanel.Enabled)
            {
                _inputPanel.Enabled = true;
                Application.DoEvents();
                Thread.Sleep(100);
            }
        }

        public void HideTextBox()
        {
            _allowTextInput = false;
            _inputPanel.Enabled = false;
        }

        private void XFPanelContainerResize(object sender, EventArgs e)
        {
            if (MainPanel == null || Parent == null)
                return;

            MainPanel.Width = Width;
            MainPanel.ShowScrollbar(MainPanel.ScrollbarActive);

            if (Header != null)
            {
                Header.Reset();
                Header.Width = Width;
            }

            MainPanel.Reset();
        }

        ~XFPanelContainer() 
        {
            if(UpdateNotification!=null)
                UpdateNotification.Dispose();
        }

        private void XFPanelContainer_LostFocus(object sender, EventArgs e)
        {
            if (mainPanel == null) return;

            mainPanel.CleanUp();

            var pan = mainPanel.LeftPanel;
            while(pan != null)
            {
                pan.CleanUp();
                pan = pan.LeftPanel;
            }

            pan = mainPanel.RightPanel;
            while (pan != null)
            {
                pan.CleanUp();
                pan = pan.RightPanel;
            }
        }
    }
#pragma warning restore 1591
}